package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.SitesModel;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Piotr
 */
public class SitesDAOImpl implements SitesDAO {

    private EntityManager entityManager;
    
    public SitesDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }
    
    @Override
    public List<SitesModel> getSiteByType(String type) {
        return (List<SitesModel>) entityManager.createNamedQuery("SitesModel.findBySite").setParameter("site", type).getResultList();
    }

    @Override
    public void addSite(SitesModel site) {
        entityManager.getTransaction().begin();
        entityManager.persist(site);
        entityManager.flush();
        entityManager.refresh(site);
        entityManager.getTransaction().commit();
    }

    @Override
    public SitesModel getSiteById(Integer id) {
        return (SitesModel) entityManager.createNamedQuery("SitesModel.findById").setParameter("id", id).getSingleResult();
    }

    @Override
    public void deleteSite(SitesModel site) {
        entityManager.getTransaction().begin();
        entityManager.remove(site);
        entityManager.getTransaction().commit();
    }
    
}
